Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor classes to use __init__ #139

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

refactor classes to use __init__ #139

wants to merge 3 commits into from

Conversation

chdsbd
Copy link
Collaborator

@chdsbd chdsbd commented Oct 6, 2022

I believe we previously used new to support Pylance. Now Pylance doesn't support new so we need to use init.

Mypy works either way, but this allows us to get Pylance working properly with auto complete.

related: #136

I believe we previously used __new__ to support Pylance. Now Pylance doesn't support __new__ so we need to use __init__.

Mypy works either way, but this allows us to get Pylance working properly with auto complete.
@sbdchd
Copy link
Owner

sbdchd commented Oct 6, 2022

oh pyright is angry in CI, maybe it needs an update?

chdsbd added 2 commits October 6, 2022 17:35
Using __init__ with array types causes a bunch of unknowns with Pylance.
def __new__( # type: ignore [misc]
cls,
def __init__(
self: JSONField[_A],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont believe an annotation is needed for self. At least I never see it in typeshed repo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the annotation is required here to handle the Optional case. You can find examples of this in the typeshed repository too:

https://cs.github.com/python/typeshed/blob/d1375f600cc4a7e7d81dcd9229c6abb933efda2a/stdlib/array.pyi?q=__init__%28self%3A#L24-L28

@bschnurr
Copy link

any eta?

@chdsbd
Copy link
Collaborator Author

chdsbd commented Oct 26, 2022

@bschnurr We can make this change, but it breaks foreign keys for Pylance. Mypy still works though: #136 (comment)

@bschnurr
Copy link

bschnurr commented Oct 31, 2022

@chdsbd found an alternative fix..

just remove def __init__(self, *args: Any, **kwargs: Any) -> None: ... from class Field and keep all the __new__ func definitions

def __init__(self, *args: Any, **kwargs: Any) -> None: ...

then the previous __new__ function signatures will be picked up

__new__ and __init__ signatures aren't matching.

pyright does have a check for this. reportInconsistentConstructor

now i see a proper signature
image

@bschnurr
Copy link

bschnurr commented Nov 2, 2022

this is a pylance bug. working on a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants